﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.ServiceLocation;
using Castle.Windsor;
using CommonServiceLocator.WindsorAdapter;

namespace $safeprojectname$
{
    public class $globalclassname$ : System.Web.HttpApplication
    {
		private static IWindsorContainer Container;

		protected void Application_Start()
		{
			GlobalFilters.Filters.Add(new HandleErrorAttribute());
			AreaRegistration.RegisterAllAreas();
			RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			RouteTable.
				Routes.
				MapRoute(
					"Default",
					"{controller}/{action}/{id}",
					new
					{
						controller = "Home",
						action = "Index",
						id = UrlParameter.Optional
					}
				);

			ServiceLocator.SetLocatorProvider(() => this.BuildProvider());
			
			//DependencyResolver.SetResolver(ServiceLocator.Current);
		}

		private IServiceLocator BuildProvider()
		{
			if (CaramelApplication.Container == null)
			{
				CaramelApplication.Container =
					new WindsorContainer().
						Register(
							// Register components here
						).
						Install(
							// Add installers here
						);
			}

			return new WindsorServiceLocator(CaramelApplication.Container);
		}
    }
}